Some of the guidelines are of the kind that are used by programming professionals. Others are of the kind that are appropriate for beginning programmers (because they improve understanding) but that are less important for professionals.
The name of the file and the name of the class must coincide
exactly. For example the Queue
class must be
in a file named Queue.java
.
Note that Java is case-sensitive (even though some versions of
MS-Windows are not).
The only exception to this rule is that all constructors must
be listed first. For example, in a Queue
class, the
constructor(s) should be first, the pop
method should
be second, and the push
method should be third.
In other words, class/instance variables should be declared at the top of the class (before any methods are declared) and variables that are local to a method should be declared at the top of that method (before any other lines of code). The only exception to this rule is that index variables in loops may be declared locally.
So, the following is discouraged:
int age = 35;
and you should, instead, do the following:
int age;
age = 35;
Within each category, public variables must be declared first, followed by protected variables, followed by package variables, followed by private variables.
For example, variables of type double
should be declared
before variables of type int
which should, in turn,
be declared before variables of type Node
.
In addition, each "word" within a class name should start
with an uppercase letter.
For example, TextMessage
and
SimpleTrafficMonitor
are both appropriate class names.
final
variables must be in
all uppercase.
Further, "words" within a "constant" name must be delimited by
an underscore character.
For example, EXTREMELY_UNHEALTHY
is an
appropriate name for a "constant".
Further, each "word" within a variable name should start
with an uppercase letter.
For example, importantMessage
and
campusMonitor
are both appropriate variable names.
In general, even single-character variable names should be lowercase. However, in some situations, mathematical notation uses uppercase letters. In such situations, uppercase variable names may be used. For example, matrices are often written using uppercase letters. So, an expression like (b = A*x) would be appropriate.
Variable names like aaa
are not appropriate.
Index variables and counters can, however, have names like
i
and j
.
Do not rely on default visibilities.
public
, protected
, private
,
abstract
, static
,
final
.
Indeed, they must have private visibility unless there is a very good reason for them not to.
This comment must describe the complete class (rather than
the methods in the class), must be in
javadoc
format, and must include:
an @author
element (containing your full name),
an @version
element (containing the date or a
version number), and the line
This work complies with the JMU Honor Code.
.
This comment must describe the method, must
be in
javadoc
format, and must include an @param
element for each formal parameter and
an @return
element if the method is
not void
. These comments must be in the
order they are listed and must not be empty.
//
rather than /* ... */
.
Instead, the operator must appear on a new line.
Except for array initializations, which must be indented 8 spaces. Also, tabs characters must not be used.
{
must appear at the end of
the line containing conditional and loop structures.
{
must appear at the end of
the line containing conditional and loop structures.
}
must appear at the start of
a new line (because it makes it easy to identify the
end of a block). The else {
, if there is one,
must appear on the same line (after a space).
switch
statements should have a
default
clause.
default
clause must be after
all case
clauses in a switch
statement.
case
clauses in switch
statements
should not fall-through
{}
)
must only be used with if
statements and loops.
Parameters in constructors and setters may.
==
or !=
operators.
for
loop control variables must not
be changed within the body of the loop.
long
literals must use an uppercase
L
(because the lowercase l
is difficult to distinguish from a 1
).
They may be used with JUnit packages.
Copyright 2024